﻿/*
Typps HTML Editor for ASP.NET
FULL EDITION

Copyright (c) 2009 Alessandro Zifiglio, http://www.Typps.com
EMail: alessandro@Typps.com

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
Typps.UpdatePanelProgress=function(element) {
    Typps.UpdatePanelProgress.initializeBase(this,[element]);
    this._backgroundColor='#000';
    this._transparency='20';
    this._retryRequestMessage='';
    this._loadingMessage='';
    this._beginRequestHandlerDelegate=null;
    this._endRequestHandlerDelegate=null;
    this._abortRequestHandlerDelegate=null;
    this._retryRequestHandlerDelegate=null;
    this._cancelHandlerDelegate=null;
    this._attachToUpdatePanelEventHandlers=null;
    this._isAborted=false;
    this._container=null;
}

Typps.UpdatePanelProgress.prototype={
    initialize: function() {
        Typps.UpdatePanelProgress.callBaseMethod(this,'initialize');
        /*IE6 & 7 expand the element to the full width of the screen, this is a known problem
        and the only fix is to have our element contained within a parent whose hasLayout is true by default eg: a table */
        var table=document.createElement('table');
        var id=this.get_id();
        table.id=id+'_container';
        var newRow=table.insertRow(0);
        var newCell=newRow.insertCell(0);
        document.body.appendChild(table);
        newCell.appendChild(this._element.parentNode.removeChild(this._element));
        this._container=table;

        this._messageContainer=$get(id+'_messagecontainer');
        this._progressImage=$get(id+'_progressimage');
        this._buttonContainer=$get(id+'_buttoncontainer');
        this._abortButton=$get(id+'_abort');
        this._cancelButton=$get(id+'_cancel');
        this._retryButton=$get(id+'_retry');
        this._buttons=[this._abortButton,this._cancelButton,this._retryButton];
        this._lastRequest=null;
        this._blockScreen=this._createBlockScreen();
        var pageManager=Sys.WebForms.PageRequestManager.getInstance();

        if(this._attachToUpdatePanelEventHandlers) {
            if(this._beginRequestHandlerDelegate==null)
                this._beginRequestHandlerDelegate=Function.createDelegate(this,this._beginRequestHandler);
            if(this._endRequestHandlerDelegate==null)
                this._endRequestHandlerDelegate=Function.createDelegate(this,this._endRequestHandler);

            pageManager.add_beginRequest(this._beginRequestHandlerDelegate);
            pageManager.add_endRequest(this._endRequestHandlerDelegate);
        }

        this._onResizeHandlerDelegate=Function.createDelegate(this,this._onResizeHandler);
        Sys.UI.DomEvent.addHandler(window,'resize',this._onResizeHandlerDelegate);

        this._onScrollHandlerDelegate=Function.createDelegate(this,this._onScrollHandler);
        Sys.UI.DomEvent.addHandler(window,'scroll',this._onScrollHandlerDelegate);

        this._abortRequestHandlerDelegate=Function.createDelegate(this,this._abortRequestHandler);
        Sys.UI.DomEvent.addHandler(this._abortButton,'click',this._abortRequestHandlerDelegate);

        this._cancelHandlerDelegate=Function.createDelegate(this,this._cancelHandler);
        Sys.UI.DomEvent.addHandler(this._cancelButton,'click',this._cancelHandlerDelegate);

        this._retryRequestHandlerDelegate=Function.createDelegate(this,this._retryRequestHandler);
        Sys.UI.DomEvent.addHandler(this._retryButton,'click',this._retryRequestHandlerDelegate);
    },
    _resizeButtonContainer: function() {
        if(this._buttonContainer==null) return;
        var width=0;
        for(var num1=0;num1<this._buttons.length;num1++) {
            var elem=this._buttons[num1];
            if(elem.style.display!='none')
                width+=elem.offsetWidth;
        }
        if(width>0) {
            this._buttonContainer.style.width=width+'px';

//            if(Sys.Browser.agent==Sys.Browser.InternetExplorer&&
//                        Sys.Browser.version==6) {
//                this._element.style.width=(width+50)+'px';
//            }
        }
    },
    _toggleCommands: function(commandType) {
        if(commandType==Typps.UpdatePanelProgress.CommandType.AllowAbort) {
            this._abortButton.style.display='';
            this._cancelButton.style.display='none';
            this._retryButton.style.display='none';
            this._progressImage.style.display='';
            this._messageContainer.innerHTML=this._loadingMessage;
        }
        else {
            this._abortButton.style.display='none';
            this._cancelButton.style.display='';
            this._retryButton.style.display='';
            this._progressImage.style.display='none';
            this._messageContainer.innerHTML=this._retryRequestMessage;
        }
        this._resizeButtonContainer();
    },
    _abortRequestHandler: function(args) {
        var h=this.get_events().getHandler('abortRequest');
        if(h) h(this,args);
        var pageManager=Sys.WebForms.PageRequestManager.getInstance();
        if(pageManager.get_isInAsyncPostBack())
            pageManager.abortPostBack();
        else
            this._isAborted=true;
        this.hideProgressBar();
        this._lastRequest=null;
    },
    _cancelHandler: function(args) {
        var h=this.get_events().getHandler('cancel');
        if(h) h(this,args);
        this.hideProgressBar();
        this._abortButton.style.display='';
        this._cancelButton.style.display='none';
        this._retryButton.style.display='none';
        this._progressImage.style.display='';
        this._messageContainer.innerHTML=this._loadingMessage;
        this._lastRequest=null;
    },
    _retryRequestHandler: function(args) {
        var h=this.get_events().getHandler('retryRequest');
        if(h) h(this,args);

        this._toggleCommands(Typps.UpdatePanelProgress.CommandType.AllowAbort);

        if(this._lastRequest!=null)
            this._makeWebRequest(null);
    },
    _makeWebRequest: function(request) {
        if(request==null) {
            request=new Sys.Net.WebRequest();
            request.set_url(this._lastRequest._url);
            var headers=this._lastRequest._headers;
            for(var h in headers)
                request.get_headers()[h]=headers[h];
            if(request._httpVerb!=null)
                request.set_httpVerb(request._httpVerb);
            request.set_timeout(this._lastRequest._timeout);
            request.set_body(this._lastRequest._body);
        }

        if(this._beginRequestHandlerDelegate==null)
            this._beginRequestHandlerDelegate=Function.createDelegate(this,this._beginRequestHandler);

        if(this._endRequestHandlerDelegate==null)
            this._endRequestHandlerDelegate=Function.createDelegate(this,this._endRequestHandler);

        request.add_completed(this._endRequestHandlerDelegate);
        this._beginRequestHandler(this,request);
        request.invoke();
    },
    add_beginRequest: function(handler) {
        this.get_events().addHandler('beginRequest',handler);
    },
    remove_beginRequest: function(handler) {
        this.get_events().removeHandler('beginRequest',handler);
    },
    add_endRequest: function(handler) {
        this.get_events().addHandler('endRequest',handler);
    },
    remove_endRequest: function(handler) {
        this.get_events().removeHandler('endRequest',handler);
    },
    _onResizeHandler: function(event) {
        this._positionProgressbar();
        this._applyBlockScreenBounds(this._blockScreen);
    },
    _onScrollHandler: function(event) {
        this._positionProgressbar();
        this._applyBlockScreenBounds(this._blockScreen);
    },
    add_abortRequest: function(handler) {
        this.get_events().addHandler('abortRequest',handler);
    },
    remove_abortRequest: function(handler) {
        this.get_events().removeHandler('abortRequest',handler);
    },
    add_cancel: function(handler) {
        this.get_events().addHandler('cancel',handler);
    },
    remove_cancel: function(handler) {
        this.get_events().removeHandler('cancel',handler);
    },

    add_retryRequest: function(handler) {
        this.get_events().addHandler('retryRequest',handler);
    },
    remove_retryRequest: function(handler) {
        this.get_events().removeHandler('retryRequest',handler);
    },
    _createBlockScreen: function() {
        var tagName='div';
        if(Sys.Browser.agent==Sys.Browser.InternetExplorer)
            tagName='iframe'; 
        var shader=document.createElement(tagName);
        shader.id=this.get_id()+'_shader';
        shader.style.display='none';
        this._applyShade(shader,this._backgroundColor,parseInt(this._transparency));
        this._applyBlockScreenBounds(shader);
        var container=document.body;
        if((Sys.Browser.agent==Sys.Browser.InternetExplorer&&
                        Sys.Browser.version==6)&&document.forms.length>0)
            container=document.forms[0];

        container.appendChild(shader);
        return shader;
    },
    _applyBlockScreenBounds: function(element) {
        var bounds=Typps.Utility.windowBounds();
        var distance=Typps.Utility.scrollDistance();
        element.style.width=bounds.ix+distance.x+'px';
        element.style.height=bounds.iy+distance.y+'px';
    },
    _applyShade: function(element,shaderColor,transparency) {
        element.style.position='absolute';
        element.style.left='0px';
        element.style.top='0px';
        element.style.backgroundColor=shaderColor;
        Typps.Utility.setTransparency(element,transparency);
    },
    showProgressBar: function() {
        this.setNewMessage(this._loadingMessage);
        var zIndex=Typps.Utility.incrementZIndex(1);
        this._blockScreen.style.display='';
        this._blockScreen.style.zIndex=zIndex;
        this._container.style.position='absolute';
        this._element.style.display='';
        this._container.style.zIndex=zIndex+1;
        this._positionProgressbar();
    },
    setNewMessage: function(newMessage) {
        var p=this._element.getElementsByTagName('p')[0];
        p.innerHTML=newMessage;
    },
    getMessage: function() {
        var p=this._element.getElementsByTagName('p')[0];
        return p.innerHTML;
    },
    hideProgressBar: function() {
        this._element.style.display='none';
        this._blockScreen.style.display='none';
    },
    _beginRequestHandler: function(sender,args) {
        this._isAborted=false; //reset
        var h=this.get_events().getHandler('beginRequest');
        if(h) h(sender,args);
        this.showProgressBar();
        this._resizeButtonContainer();
        if(args['_request']!=null)
            this._lastRequest=args['_request']; // updatepanel
        else
            this._lastRequest=args; //Sys.Net.WebRequest manual
    },
    _endRequestHandler: function(sender,args) {
        if(this._isAborted) {
            this._isAborted=false; //reset
            this._lastRequest=null;
            this.hideProgressBar();
            return;
        }
        //use sender.get_responseAvailable() for all failures, reference msdn.
        var isTimedOut=false;
        if(sender.get_timedOut!=undefined)
            isTimedOut=sender.get_timedOut();
        else if(args.get_error!=undefined)
            isTimedOut=(args.get_error()!=null&&
            args.get_error().name=='Sys.WebForms.PageRequestManagerTimeoutException');

        if(isTimedOut) {
            if(args.set_errorHandled!=undefined)
                args.set_errorHandled(true);
            this._toggleCommands(Typps.UpdatePanelProgress.CommandType.AllowRetry);
        }
        else {
            this._toggleCommands(Typps.UpdatePanelProgress.CommandType.AllowAbort);
            this.hideProgressBar();
            this._lastRequest=null;
        }
        var h=this.get_events().getHandler('endRequest');
        if(h) h(sender,args);
    },
    _positionProgressbar: function() {
        if(this._container==null) return;
        var bounds=Typps.Utility.windowBounds();
        var distance=Typps.Utility.scrollDistance();
        var x=(bounds.ix-this._container.offsetWidth)/2;
        var y=(bounds.iy-this._container.offsetHeight)/2;
        this._container.style.left=(x+distance.x)+'px';
        this._container.style.top=(y+distance.y)+'px';
    },
    get_attachToUpdatePanelEventHandlers: function() {
        return this._attachToUpdatePanelEventHandlers;
    },
    set_attachToUpdatePanelEventHandlers: function(value) {
        this._attachToUpdatePanelEventHandlers=value;
    },
    get_backgroundColor: function() {
        return this._backgroundColor;
    },
    set_backgroundColor: function(value) {
        this._backgroundColor=value;
    },
    get_loadingMessage: function() {
        return this._loadingMessage;
    },
    set_loadingMessage: function(value) {
        this._loadingMessage=value;
    },
    get_retryRequestMessage: function() {
        return this._retryRequestMessage;
    },
    set_retryRequestMessage: function(value) {
        this._retryRequestMessage=value;
    },
    get_transparency: function() {
        return parseInt(this._transparency);
    },
    set_transparency: function(value) {
        this._transparency=value;
    },
    dispose: function() {
        if(this._beginRequestHandlerDelegate) {
            delete this._beginRequestHandlerDelegate;
        }
        if(this._beginRequestHandlerDelegate) {
            delete this._endRequestHandlerDelegate;
        }
        Typps.UpdatePanelProgress.callBaseMethod(this,'dispose');
    }
}
Typps.UpdatePanelProgress.registerClass('Typps.UpdatePanelProgress',Sys.UI.Control);

Typps.UpdatePanelProgress.CommandType=function() { };
Typps.UpdatePanelProgress.CommandType.prototype=
{
    AllowRetry: 0,
    AllowAbort: 1
}
Typps.UpdatePanelProgress.CommandType.registerEnum("Typps.UpdatePanelProgress.CommandType");

// Since this script is not loaded by System.Web.Handlers.ScriptResourceHandler
// invoke Sys.Application.notifyScriptLoaded to notify ScriptManager 
// that this is the end of the script.
if(typeof (Sys)!=='undefined') Sys.Application.notifyScriptLoaded();
